# # https://pcs.baidu.com/rest/2.0/pcs/file?method=upload&path=上传后的文件名及路径（URL编码）access_token=23.256acf91eb74663bf2eec5c9d33f1e1c.2592000.1576558530.3710620705-238347
# import json
# import os
# import requests
# from requests_toolbelt import MultipartEncoder
#
# filename = 'QR.png'
# fsize = os.path.getsize(filename)
# m = MultipartEncoder(
#     fields={'f_path': '/home/wechat/', 'f_name': 'QR.png',
#             'blob': ('filename', open(filename,"rb"), 'text/plain'),
#             'f_size':str(fsize),
#             "f_start":str(0)
#             }
# )
# headers={
#     "Cookie": "order=id%20desc; memSize=992; sites_path=/www/wwwroot; serverType=apache; uploadSize=1073741824; backup_path=/www/backup; force=0; pnull=1; softType=5; BT_PANEL_6=e13edde7-ece9-49e5-9ef0-03989a5c5106.2j7AOpeAVc0MajvSxL7lGP-2Z1U; request_token=fqJ0hVBPkb1imRapQvFQ4omfLEwrq8uUDLiiDlyxVycMPytn; rank=a; Path=/home/wechat"
#     ,"Host": "134.175.106.211:8888"
#     ,"Origin": "http://134.175.106.211:8888"
#     ,"Referer": "http://134.175.106.211:8888/files"
#     ,"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
#     ,"x-cookie-token": "fqJ0hVBPkb1imRapQvFQ4omfLEwrq8uUDLiiDlyxVycMPytn"
#     ,"x-http-token": "l2tWl2JwlDGFWaYzmZDIMqOlknAjQ53OaWR4pN9hGvZ26nW3"
#     ,"Content-Type": m.content_type
# }
# resp = requests.post("http://134.175.106.211:8888/files?action=upload",headers=headers,data=m)
# resp = json.loads(resp.text)
# if resp['status'] == True:
#     print("dddddddddd")
# else:
#     print("xxxx")

import PIL.ImageGrab
import os
from PIL import Image
def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024
def compress_image(infile, outfile='', mb=150, step=10, quality=80):
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标，KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址，压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        return infile
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)
#
# filename = "WindowsCapture.png"
# im=PIL.ImageGrab.grab()
# im.save(filename)
# compress_image(filename)

print(PIL.__version__)
